<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Favorite extends FOX_Controller {

	function __construct ()
	{
		parent::__construct();
		$this->load->model('user_m');
		$this->load->model('member_m');
		$this->load->model('find_m');
		$this->load->model('article_m');
		$this->load->library('form_validation');
		$this->config->load('userset');		
	}
	
	public function favorite(){
		$good_id = $this->input->post('item_id');
        $user_id = $user_id=$this->session->userdata('user_id');
        $goods=$this->member_m->get_goods_by_goods_id($good_id);
        if(empty($goods)){
        	$msg['info'] = "参数错误";
	        $msg['status'] = 1;
	        echo json_encode($msg);
        }
        if($user_id && $goods){
	        $fav_status= $this->member_m->get_favorite($user_id,$good_id);
	        if($fav_status){
	        	$favorits=$this->member_m->del_favorite($good_id,$user_id);
	        	$msg['info'] = "取消收藏成功";
	         	$msg['status'] = 0;
	            echo json_encode($msg);
	        }else{
		        $data['user_id'] = $user_id;
		        $data['item_id'] = $goods['good_id'];
		        $data['item_type'] = 0;
		        $data['favorite_time'] = time();
		        
		        $favorite = $this->member_m->add_favorites($data);
		        $msg['info'] = "收藏商品成功";
		        $msg['status'] = 1;
		        echo json_encode($msg);
	        }
	    }else{
	    	$msg['info'] = "您还没有登录请先登录";
	        $msg['status'] = 2;
	        echo json_encode($msg);
	    }

	}
	//收藏文章
	public function article_favorite(){
		$article_id = $this->input->post('item_id');
        $user_id = $this->session->userdata('user_id');
        $articles= $this->find_m->get_articles($article_id);
        if(empty($articles)){
        	$msg['info'] = "参数错误";
	        $msg['status'] = 1;
	        echo json_encode($msg);
        }
        if($user_id && $articles){
	        $fav_status= $this->find_m->get_article_favorite($user_id,$article_id);
	        if($fav_status){
	        	$favorits=$this->find_m->del_article_favorite($user_id,$article_id);
	        	$msg['info'] = "取消收藏成功";
	         	$msg['status'] = 0;
	            echo json_encode($msg);
	        }else{
		        $data['user_id'] = $user_id;
		        $data['item_id'] = $article_id;
		        $data['item_type'] = 2;
		        $data['favorite_time'] = time();
		        
		        $favorite = $this->member_m->add_favorites($data);
		        $msg['info'] = "收藏文章成功";
		        $msg['status'] = 1;
		        echo json_encode($msg);
	        }
	    }else{
	    	$msg['info'] = "您还没有登录请先登录";
	        $msg['status'] = 2;
	        echo json_encode($msg);
	    }

	}

	//给文章点赞
	public function love_article(){
		$article_id = $this->input->post('item_id');
        $user_id = $this->session->userdata('user_id');
        $articles= $this->find_m->get_articles($article_id);
        //$articlePointadd = $articlecon['article_love'];
        $data['article_love'] = $articles['article_love']+1;
        $result= $this->article_m->sava_article($article_id,$data);
        if( $result){
	        //更新点赞数据
	        $msg['info'] = "点赞成功";
	        $msg['status'] = 1;
	        echo json_encode($msg);
	    }
	}
	//从列表中移除已收藏的商品
	public function delfavorite(){
		$favorite_id = $this->input->post('favorite_id');
		$user_id = $this->session->userdata('user_id');
		if($user_id){
			$favorits=$this->member_m->del_favorites($favorite_id,$user_id);
			if($favorits){
				$msg['info'] = "移除收藏成功";
	         	$msg['status'] = 0;
	           	echo json_encode($msg);
			}else{
				$msg['info'] = "移除收藏失败";
	         	$msg['status'] = 1;
	           	echo json_encode($msg);
			}
	        
		}
	}

	
}
